

 
TARGET =boot

BUILD_DIR =output

######################################
# source
######################################
# C sources

C_SRC =$(wildcard *.c)
 
# ASM sources
ASM_SRC =$(wildcard *.s)
  
INC_PATH = -Iinc

LDS = F1C100S_BROM.ld
 
CROSS_COMPILE ?= arm-none-eabi-
 

# host compiler always use gcc 
HOST_CC =gcc
#MKBOOTHEAD=../mksunxi/mksunxi  
MKBOOTHEAD=$(BUILD_DIR)/mksunxi
MSSUNXI_CSRC=../mksunxi/mkboothead.c
MKSUNXI_TARGET=mksunxi


# cross toolchain
CC = $(CROSS_COMPILE)gcc
AS = $(CROSS_COMPILE)gcc -x assembler-with-cpp
CP = $(CROSS_COMPILE)objcopy
SZ = $(CROSS_COMPILE)size
CPP= $(CROSS_COMPILE)g++
LD = $(CROSS_COMPILE)ld
DP = $(CROSS_COMPILE)objdump
 
HEX = $(CP) -O ihex
BIN = $(CP) -O binary -S


# parameters for toolchain
CPU = arm926ej-s
ARCH = armv5te
DEF = -D__ARM926EJS__ -D__ARM32_ARCH__=5
CFLAGS = -mtune=$(CPU) -march=$(ARCH) -mthumb
CFLAGS += -Wall -fdata-sections -ffunction-sections -ffreestanding -flto -g
CFLAGS += $(DEF)
ASMFLAGS = -g -ggdb -Wall -march=armv5te -mfloat-abi=soft

LDFLAGS = -mtune=$(CPU) -march=$(ARCH) -lgcc 
LDFLAGS += -nostdlib  -Wl,--gc-sections


# get all object files
OBJS = $(addprefix $(BUILD_DIR)/,$(notdir $(C_SRC:.c=.o)))
OBJS += $(addprefix $(BUILD_DIR)/,$(notdir $(ASM_SRC:.s=.o)))
#vpath %.c $(addsuffix /src,$(SRC_PATH))
#vpath %.s $(addsuffix /src,$(SRC_PATH))

# building rules
.PHONY:all clean test download

all:$(BUILD_DIR) $(MKSUNXI_TARGET) $(BUILD_DIR)/$(TARGET).elf $(BUILD_DIR)/$(TARGET).bin
 
$(MKSUNXI_TARGET):$(MSSUNXI_CSRC) $(BUILD_DIR)
	$(HOST_CC) $< -o $(BUILD_DIR)/$@

# if SUB_BUILD_DIR not exists
$(BUILD_DIR): 
	mkdir $@

# build .c files
$(BUILD_DIR)/%.o: %.c  
	$(CC) -c $(CFLAGS)  $<  $(INC_PATH) -o $@ 

# build .s files
$(BUILD_DIR)/%.o: %.s 
	$(AS)  -c $(CFLAGS) $<  $(INC_PATH) -o $@
 
# linking *.elf
$(BUILD_DIR)/$(TARGET).elf : $(OBJS)
	$(CC) $^ $(LDFLAGS) -T$(LDS) -o $@
	$(SZ) $@
	$(DP) -D -S $@  > $(@:.elf=.txt)

# exporting *.bin from *.elf
$(BUILD_DIR)/$(TARGET).bin : $(BUILD_DIR)/$(TARGET).elf
	$(CP) -O binary -S $< $@
	$(MKBOOTHEAD) $@
 
clean: 
	rm -rf $(BUILD_DIR)

download:
	xfel spinor write 0 $(BUILD_DIR)/$(TARGET).bin

test:
	@echo $(C_SRC) $(ASM_SRC) 
	@echo $(OBJS)